home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume4 / chuni < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  5.3 KB

  1. From: panda!talcott!seismo!munnari!aaec.oz!frank (Frank Crawford)
  2. Subject: chuni.c - change a users default universe (Pyramid specific)
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 4, Issue 3
  7. Submitted by: panda!talcott!seismo!munnari!aaec.oz!frank (Frank Crawford)
  8.  
  9.  
  10. ----------------- CUT HERE ------------------- CUT HERE ---------------------
  11. #!/bin/sh
  12. # This is a shell archive, extract with sh (not csh)
  13. # It contains :    README
  14. #        Makefile
  15. #        chuni.1
  16. #        chuni.c
  17. #
  18. echo 'Start of chuni.shar, part 01 of 01:'
  19. echo 'x - README'
  20. sed 's/^X//' > README << '/'
  21. X    Here is a program to let users change their default login universe.
  22. XIt's Pyramid specific and is used in a fashion similar to ``chsh''.
  23. X
  24. X    This program was written very early on and as much as anything was
  25. Xreally to play with file locking and with universes.  It has been used on
  26. Xour Pyramid for nearly 18 months and hasn't been changed in over 12 months.
  27. X
  28. X    The major problem with it is that it relies on the Universe name
  29. Xbeing only three characters long.  If this ever changes (and it probably
  30. Xwill) then the program will have to be rewritten.
  31. X
  32. X    To use it just compile and install it setuid root.  See the
  33. Xaccompanying Makefile.
  34. X
  35. X    Any bugs or problems let me know.
  36. X
  37. X                        Frank Crawford
  38. X
  39. X------
  40. XMail:    Australian Atomic Energy Commission,
  41. X    Private Mailbag,
  42. X    Sutherland,
  43. X    N.S.W.    2232
  44. X    Australia.
  45. XPhone:    +61 2 543 3094
  46. X
  47. XACSnet:    frank@aaec.OZ
  48. XCSNET:    frank@aaec.OZ
  49. XUUCP:    {seismo,mcvax,ukc,prlb2,ubc-vision}!munnari!aaec.OZ!frank
  50. XARPA/
  51. XInternet: frank%aaec.OZ@seismo.CSS.GOV
  52. /
  53. echo 'x - Makefile'
  54. sed 's/^X//' > Makefile << '/'
  55. X#    Makefile for chuni
  56. XPROGNAME = chuni
  57. X
  58. X# programs
  59. XCC = cc
  60. X
  61. X# flags
  62. XCFLAGS = -O
  63. XLDFLAGS =
  64. X
  65. X# directories
  66. XBIN = /usr/local/bin
  67. X
  68. X#files
  69. XSRCS = chuni.c
  70. X
  71. Xall:    $(PROGNAME)
  72. X
  73. X$(PROGNAME):    $(SRCS)
  74. X    $(CC) $(CFLAGS) $(LDFLAGS) -o $(PROGNAME) $(SRCS)
  75. X
  76. Xinstall:    $(PROGNAME)
  77. X    ucb install -o root -m u+srwx,og+rx $(PROGNAME) $(BIN)
  78. X
  79. Xclean:
  80. X    rm -f core *.o $(PROGNAME)
  81. X
  82. Xdelta:
  83. X    sccs delta `sccs tell`
  84. X
  85. Xprint:
  86. X    @print Makefile $(SRCS)
  87. /
  88. echo 'x - chuni.1'
  89. sed 's/^X//' > chuni.1 << '/'
  90. X.TH CHUNI 1 local
  91. X.SH NAME
  92. Xchuni \- change default login universe
  93. X.SH SYNOPSIS
  94. X.B chuni
  95. Xname
  96. X[
  97. Xuniverse
  98. X]
  99. X.SH DESCRIPTION
  100. X.I Chuni
  101. Xis a command similar to
  102. X.IR  chsh (1) ,
  103. Xexcept that it is used to change the login universe field in the universe
  104. Xfile rather than the shell field in the password file.
  105. XIf no
  106. X.I universe
  107. Xis specified,
  108. Xthen the current login universe is printed.
  109. XOtherwise, only
  110. X.IR att
  111. Xor
  112. X.I ucb
  113. Xcan be specified as the universe.
  114. X.PP
  115. XOnly the Superuser can change the login universe of someone else.
  116. X.PP
  117. XAn example use of this command would be
  118. X.PP
  119. X.DT
  120. X    chuni bill ucb
  121. X.SH FILES
  122. X/etc/u_universe
  123. X.SH "SEE ALSO"
  124. Xchsh(1), universe(1) 
  125. X.SH AUTHOR
  126. XFrank Crawford
  127. X.\"    @(#)chuni.1    8/31/84
  128. X.SH BUGS
  129. X.PP
  130. XIf Pyramid ever add a universe name that is not 3 characters long then
  131. Xthis will have to be rewritten.
  132. /
  133. echo 'x - chuni.c'
  134. sed 's/^X//' > chuni.c << '/'
  135. X/* This program is designed to change the default login universe
  136. X * of a user.  It requires to run setuid and handles checking of
  137. X * user permissions (hopefully).
  138. X *
  139. X *    Only a user or root can change a the login universe
  140. X *                    F. Crawford - 31 Jul 84
  141. X *    SCCS @(#)chuni.c    1.3    9/26/84
  142. X */
  143. X#include <stdio.h>
  144. X#include <pwd.h>
  145. X#include <universe.h>
  146. X#include <sys/file.h>
  147. X
  148. X#define UNIVERSE    "/etc/u_universe"    /* Universe file */
  149. X#define NOUNIV    (sizeof(univ_name) / sizeof(char *))
  150. X                        /* Number of known universes */
  151. X#define UCB    2                /* Universe number of UCB */
  152. X
  153. Xchar *getlogin();
  154. X
  155. Xvoid release (file)
  156. X    FILE *file;
  157. X    {
  158. X    flock(fileno(file), LOCK_UN);
  159. X    fclose(file);
  160. X    }
  161. X
  162. Xint main(argc, argv)
  163. X    int argc;
  164. X    char **argv;
  165. X    {
  166. X    register FILE *univ;
  167. X    register char *loginid;
  168. X    register int i;
  169. X    char buf[20], name[20];
  170. X    int len;
  171. X    struct passwd *pwdent;
  172. X
  173. X    setuniverse(UCB);
  174. X    if (--argc < 1)
  175. X    {
  176. X    fprintf(stderr, "Usage: %s user [universe]\n", *argv);
  177. X    exit(1);
  178. X    }
  179. X    else
  180. X    ++argv;
  181. X    if (!(loginid = getlogin()))
  182. X    {
  183. X    if (!(pwdent = getpwuid(getuid())))
  184. X        {
  185. X        fprintf(stderr, "You don't exist\n");
  186. X        exit(1);
  187. X        }
  188. X    loginid = pwdent->pw_name;
  189. X    }
  190. X    if (!(univ = fopen(UNIVERSE, "r+")))
  191. X    {
  192. X    perror(UNIVERSE);
  193. X    exit(1);
  194. X    }
  195. X    setbuf(univ, NULL);
  196. X    if (flock(fileno(univ), LOCK_SH | LOCK_NB) < 0)
  197. X    {
  198. X    fprintf(stderr, "File is busy\n");
  199. X    fclose(univ);
  200. X    exit(1);
  201. X    }
  202. X    strcpy(name, *argv);
  203. X    strcat(name, ":");
  204. X    len = strlen(name);
  205. X    while (fgets(buf, sizeof(buf), univ) && strncmp(name, buf, len))
  206. X    ;
  207. X    if (strncmp(name, buf, len))
  208. X    {
  209. X    fprintf(stderr, "Name not found\n");
  210. X    release(univ);
  211. X    exit(1);
  212. X    }
  213. X    if (argc == 1)
  214. X    {
  215. X    printf("%s", &buf[len]);
  216. X    release(univ);
  217. X    exit(0);
  218. X    }
  219. X    else
  220. X    {
  221. X    if (getuid() && strcmp(*argv, loginid))
  222. X        fprintf(stderr, "Permission denied\n");
  223. X    else
  224. X        {
  225. X        for (i = 0; i < NOUNIV; i++)
  226. X        if (!strcmp(argv[1], univ_name[i]))
  227. X            {
  228. X            flock(fileno(univ), LOCK_EX);
  229. X            fseek(univ, (long) (-strlen(buf)), 1);
  230. X            fprintf(univ, "%s:%s\n", argv[0], argv[1]);
  231. X            release(univ);
  232. X            exit(0);
  233. X            }
  234. X        fprintf(stderr, "Unknown universe\n");
  235. X        }
  236. X    }
  237. X    release(univ);
  238. X    exit(1);
  239. X    }
  240. /
  241. echo 'Part 01 of chuni.shar complete.'
  242. exit
  243.  
  244.